//
//  HMDiscoverySearchView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/5.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit

class HMDiscoverySearchView: UIView ,UITextFieldDelegate{


    @IBOutlet weak var cancelButton: UIButton!

    @IBOutlet weak var searchTextField: UITextField!

    @IBOutlet weak var leftConstraint: NSLayoutConstraint!

    
    @IBAction func cancelClick(sender: AnyObject) {
        
        searchTextField.resignFirstResponder()
        leftConstraint.constant = 0
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.searchTextField.layoutIfNeeded()
        }
        
    }

    
    class func searchView() -> HMDiscoverySearchView {
        
        let view = NSBundle.mainBundle().loadNibNamed("HMDiscoverySearchView", owner: nil, options: nil).last as! HMDiscoverySearchView

        return view
    
    }

    override func awakeFromNib() {
        
        leftView.frame = CGRectMake(0, 0, frame.height, frame.height)
        leftView.contentMode = .Center
        searchTextField.leftView = leftView
        searchTextField.leftViewMode = UITextFieldViewMode.Always
        
        searchTextField.layer.borderWidth = 5
        searchTextField.layer.cornerRadius = 10;
        searchTextField.layer.borderColor = UIColor.redColor().CGColor
        
    }

    
    /**
     textField的代理方法
     - parameter textField: textFiled本身
     */
    func textFieldDidBeginEditing(textField: UITextField){
    
        leftConstraint.constant = cancelButton.frame.width
        // 添加动画
        UIView.animateWithDuration(1) { () -> Void in
            self.searchTextField.layoutIfNeeded()
        }
        
    }
    
    
    private lazy var leftView: UIImageView = {
    
        return UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))

    }()

}
